package toolkits;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class RandomString {
    
    private static final String lower_case = "abcdefghijklmnopqrstuvwxyz";
    private static final String upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String number = "1234567890";
    private static final String hexadecimal = "1234567890ABCDEF";

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(hexadecimal);
        System.out.println(generateRandom(hexadecimal, 16));
        //String taskid="141434587286231";
        //System.out.println(md5(taskid,"import121"));
    }
    
    private static String generateRandom(String pool, int length){
        Random rand = new Random();
        char[] passwordChar = new char[length];
        for (int i = 0; i < length; i++) {
                passwordChar[i] = pool.charAt(rand.nextInt(pool.length()));
        }
        
        return new String(passwordChar);
    }
    
    public static String md5(String s, String md5String)
    { 
       if (md5String == null || s==null)
       {
           System.out.println("md5 error: output string is null");
           return "";
       }
      
       try {
           MessageDigest md = MessageDigest.getInstance( "MD5" );
    
           md.reset();
           md.update( s.getBytes() );
           byte[] arr = md.digest();

           for (int i = 0; i < arr.length; i++) 
           {
               if (arr[i]<16 && arr[i]>=0)
               {
                   md5String += "0";
                   md5String += Integer.toHexString(arr[i]&0xff);
               }
               else
                   md5String += Integer.toHexString(arr[i]&0xff);
           }
       } catch (NoSuchAlgorithmException e) {
           System.out.println("md5 error:"+e.toString());
       }
   
       return md5String;
    }

}
